package function;

import Mysql.ShareData;
import View.MainWindow;
import View.AddFriendWindow;
import View.window;
import client.ClientManager;
import instruction.Friend;
import instruction.Member;
import instruction.Message;
import instruction.User;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;


public class AddFriendFunction {
    public static Object $(window window, String id) {
        return (Object) window.getRoot().lookup("#" + id);
    }
    public static void searchFriendExec(AddFriendWindow addFriendWindow, MainWindow mainWindow, ClientManager clientManager){
        ((Label)$(addFriendWindow,"errorTip")).setText("");
        ((Button)$(addFriendWindow,"addButton")).setOnAction(event -> {
            boolean flag=true;
            User user=mainWindow.getUserData().getUser();
            String userName=user.getName();
            String userAccount=user.getAccount();
            String userImage=user.getHead();
            String userNickname=user.getNickname();
            String userAge=user.getAge();
            String userSex=user.getSex();
            String userBirthday=user.getBirthday();
            String userSign=user.getSignature();
            String userPhone=user.getPhone();
            String userEmail=user.getEmail();
            String userAddress=user.getAddress();
            Member userMember=new Member(userName,userAccount,userSex,userBirthday,userSign,userEmail,userAge,userAddress,userNickname,userPhone,userImage);
            User friend=addFriendWindow.getFriend();
            String friendAccount=friend.getAccount();
            String friendImage=friend.getHead();
            String friendName=friend.getName();
            String friendNickname= friend.getNickname();
            String friendAge=friend.getAge();
            String friendSex=friend.getSex();
            String friendBirthday=friend.getBirthday();
            String friendSign=friend.getSignature();
            String friendEmail=friend.getEmail();
            String friendPhone=friend.getPhone();
            String friendAddress=friend.getAddress();
            Member friendMember=new Member(friendName,friendAccount,friendSex,friendBirthday,friendSign,friendEmail,friendAge,friendAddress,friendNickname,friendPhone,friendImage);
            Friend friend1=new Friend(userAccount,friendNickname,friendMember);
            Friend friend2=new Friend(friendAccount,userNickname,userMember);
            Message message1=new Message("addFriend");
            Message message2=new Message("addFriend");
            message1.setFriend(friend1);
            message2.setFriend(friend2);
            if(friend.getAccount().equals(userAccount))
            {
                flag=false;
            }
            for(Friend friend3:mainWindow.getUserData().getFriendList()){
                if(friend3.getMember().getAccount().equals(friendAccount)) {
                    flag=false;
                    break;
                }
            }
            if(flag) {
                clientManager.getSend().send(message1);
                clientManager.getSend().send(message2);
                addFriendWindow.hide();
            }
            else
            {
                ((Label)$(addFriendWindow,"errorTip")).setText("您不能重复添加好友");
            }
        });
    }
}